package ${package}.controller;

import me.exchange.com.aop.log.Log;
import me.exchange.com.utils.Result;
import ${package}.domain.${className};
import ${package}.service.${className}Service;
import ${package}.dto.${className}Dto;
import ${package}.dto.${className}QueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ${author}
* @date ${date}
*/
@Api(tags = "${apiAlias}管理")
@RestController
@RequestMapping("/api/${changeClassName}")
public class ${className}Controller {

    private final ${className}Service ${changeClassName}Service;

    public ${className}Controller(${className}Service ${changeClassName}Service) {
        this.${changeClassName}Service = ${changeClassName}Service;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @PostMapping(value = "/list/download")
    @PreAuthorize("@el.check('${changeClassName}:list')")
    public void download(HttpServletResponse response, ${className}QueryCriteria criteria) throws IOException {
        ${changeClassName}Service.download(${changeClassName}Service.queryAll(criteria), response);
    }

    @PostMapping("/list")
    @Log("查询${apiAlias}")
    @ApiOperation("查询${apiAlias}")
    @PreAuthorize("@el.check('${changeClassName}:list')")
    public ResponseEntity<Object> get${className}s(${className}QueryCriteria criteria, Pageable pageable){
        Object ${changeClassName} =  ${changeClassName}Service.queryAll(criteria,pageable);
        return new ResponseEntity<>(Result.ok("${apiAlias}查询成功").put("${changeClassName}List",${changeClassName}),HttpStatus.OK);
    }

    @PostMapping("/queryInfo")
    @Log("查询${apiAlias}详细")
    @ApiOperation("查询${apiAlias}详细")
    @PreAuthorize("@el.check('${changeClassName}:list')")
    public ResponseEntity<Object> queryInfo(Long id){
        ${className}Dto ${changeClassName} =  ${changeClassName}Service.findById(id);
        return new ResponseEntity<>(Result.ok("${apiAlias}详请查询成功").put("${changeClassName}",${changeClassName}),HttpStatus.OK);
    }

    @PostMapping("/add")
    @Log(value="新增${apiAlias}",code="")
    @ApiOperation("新增${apiAlias}")
    @PreAuthorize("@el.check('${changeClassName}:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ${className} resources){
        ${changeClassName}Service.create(resources);
        return new ResponseEntity<>(Result.ok("${apiAlias}新增成功"),HttpStatus.OK);
    }

    @PostMapping("/update")
    @Log(value="修改${apiAlias}",code="")
    @ApiOperation("修改${apiAlias}")
    @PreAuthorize("@el.check('${changeClassName}:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ${className} resources){
        ${changeClassName}Service.update(resources);
        return new ResponseEntity<>(Result.ok("${apiAlias}修改成功"),HttpStatus.OK);
    }

    @Log(value="删除${apiAlias}",code="")
    @ApiOperation("删除${apiAlias}")
    @PreAuthorize("@el.check('${changeClassName}:del')")
    @PostMapping("/del")
    public ResponseEntity<Object> delete(@RequestBody ${className}QueryCriteria criteria) {
        ${changeClassName}Service.deleteAll(criteria.getIds());
        return new ResponseEntity<>(Result.ok("${apiAlias}删除成功"),HttpStatus.OK);
    }
}